package com.commons.DBPool;

import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.game.logger.DBLogger;
import com.jolbox.bonecp.BoneCP;
import com.jolbox.bonecp.BoneCPConfig;
/**
 * 数据库连接池
 * 提供Pool单例对象
 * 获取数据库操作连接
 * @author CHENLEI
 *
 */
public class DBPoolFactory {
	private static  DBPoolFactory Pool=new DBPoolFactory();
	private BoneCP ConnPool=null;
	private DBPoolFactory(){}
	public static DBPoolFactory getPoolInstance(){
		return Pool;
	}
	/***初始化数据库连接池**/
	public void IntiPool() throws Exception{
			Class.forName("com.mysql.jdbc.Driver");
			InputStream in=new FileInputStream("res/bonecp-config.xml");
			BoneCPConfig config=new BoneCPConfig(in, "bonecp-config.xml");
			ConnPool=new BoneCP(config);
			DBLogger.logger.info("bonecp pool is Intilazition ......");
	}
	/***从BoneCp连接池中获取可操作的连接**/
	public  Connection getConnection(){
		Connection Conn=null;
		try {
			Conn=ConnPool.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return Conn;
	}
	/**递交给BoneCp中ConnectionReleaseHelperThread线程处理*/
	 public void putConnectionAndStateMent(Connection conn,PreparedStatement statement){
		 try {
			conn.close();
			statement.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }
}
